package Pet.Management.controller;

import Pet.Management.domain.dto.NoticeDTO;
import Pet.Management.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 获取公告列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getNoticeList(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 创建分页对象
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        
        // 获取分页数据
        Page<NoticeDTO> page = noticeService.getNoticeList(title, status, pageRequest);
        
        Map<String, Object> response = new HashMap<>();
        response.put("total", page.getTotalElements());
        response.put("rows", page.getContent());
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取公告详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getNoticeById(@PathVariable Long id) {
        NoticeDTO notice = noticeService.getNoticeById(id);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", notice);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 新增公告
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addNotice(@RequestBody NoticeDTO noticeDTO) {
        // 在实际应用中，应该从认证信息中获取用户ID
        // 这里简化处理，使用ID为1的用户作为发布者
        NoticeDTO addedNotice = noticeService.addNotice(noticeDTO, 1L);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "添加成功");
        response.put("data", addedNotice);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 修改公告
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> updateNotice(@RequestBody NoticeDTO noticeDTO) {
        NoticeDTO updatedNotice = noticeService.updateNotice(noticeDTO);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "修改成功");
        response.put("data", updatedNotice);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteNotice(@PathVariable Long id) {
        boolean result = noticeService.deleteNotice(id);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", result ? 200 : 500);
        response.put("msg", result ? "删除成功" : "删除失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 批量删除公告
     */
    @DeleteMapping("/batch/{ids}")
    public ResponseEntity<Map<String, Object>> deleteNotices(@PathVariable String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        
        boolean result = noticeService.deleteNotices(idList);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", result ? 200 : 500);
        response.put("msg", result ? "删除成功" : "删除失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发布公告
     */
    @PutMapping("/publish/{id}")
    public ResponseEntity<Map<String, Object>> publishNotice(@PathVariable Long id) {
        NoticeDTO publishedNotice = noticeService.publishNotice(id);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "发布成功");
        response.put("data", publishedNotice);
        
        return ResponseEntity.ok(response);
    }
} 